package com.fastloan.jn.mgt.customermanager.daily.service.impl;

import com.fastloan.jn.common.BasicServiceImpl;
import com.fastloan.jn.mgt.customermanager.daily.dao.JnCustomerManagerDailyMapper;
import com.fastloan.jn.mgt.customermanager.daily.model.JnCustomerManagerDaily;
import com.fastloan.jn.mgt.customermanager.daily.model.vo.CustomerDaliy;
import com.fastloan.jn.mgt.customermanager.daily.service.JnCustomerManagerDailyService;
import com.kawins.supers.table.manager.DataTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanwe
 * createTime 2017-10-2017/10/19 10:50
 */
@Service
public class JnCustomerManagerDailyServiceImpl extends BasicServiceImpl<JnCustomerManagerDaily> implements JnCustomerManagerDailyService {

    @Autowired
    private JnCustomerManagerDailyMapper jnCustomerManagerDailyMapper;

    @Override
    @Transactional
    public boolean addDaily(JnCustomerManagerDaily jnCustomerManagerDaily){
        int result = jnCustomerManagerDailyMapper.updateByPrimaryKeySelective(jnCustomerManagerDaily);
        return result == 1;
    }

    @Override
    public JnCustomerManagerDaily getJnCustomerManagerDailyByUserId(String userId) {
        Example example = new Example(JnCustomerManagerDaily.class);
        example.createCriteria().andEqualTo("userId",userId)
                .andCondition("TO_CHAR (JN_CUSTOMER_MANAGER_DAILY.CREATE_TIME,'YYYY-MM-DD')" +
                        " = TO_CHAR (SYSDATE, 'YYYY-MM-DD')");
        List<JnCustomerManagerDaily> jnCustomerManagerDailies = selectByExample(example);
        if (jnCustomerManagerDailies.size() != 1){
            String msg = "日报数据错误！userId:"+userId;
            log.error(msg);
            throw new RuntimeException(msg);
        }
        return jnCustomerManagerDailies.get(0);
    }

    @Override
    @DataTable(name = "managerDaliy")
    public List<CustomerDaliy> managerDaliy(Map<String,Object> map) {
        return jnCustomerManagerDailyMapper.daliyExt(map);
    }

    @Override
    public List<String> topOrgManagerIds(String topId) {
        Map<String,Object> map = new HashMap<>();
        map.put("topOrgId", topId);
        return jnCustomerManagerDailyMapper.topOrgManagerIds(map);
    }
}
